package cn.com.self.apis.login;

import cn.com.self.apis.portal.service.intf.ILoginService;
import cn.com.self.apis.util.Base64Util;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
    * CMS栏目表 前端控制器
    * </p>
*
* @since 2019-09-02
*/
@Controller("login.loginController")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    //@RequestMapping(value = {"/oauth/token"}, method = RequestMethod.GET)
    public ResponseEntity list(HttpServletRequest request) {
        Map<String, Object> result =new HashMap<String, Object>();
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap());
        String clientId=String.valueOf(queryMap.get("client_id"));
        String clientSecret=String.valueOf(queryMap.get("client_secret"));
        if(StringUtils.isNotBlank(clientId)&&"c1".equals(clientId)){
            if (StringUtils.isNotBlank(clientSecret)&&"s1".equals(clientSecret)) {
                request.getSession(true);
                result.put("code",200);
                result.put("text","success");
                result.put("data", Base64Util.encode((clientId+clientSecret).getBytes()));
            }else{
                result.put("code",401);
                result.put("text","未授权");
                result.put("code",200);
            }

        }else{
            result.put("code",401);
            result.put("text","未授权");
            result.put("code",200);
        }
        return ResponseEntity.status(HttpStatus.OK).body(result);
    }


     /**登录操作
      * @Description //TODO
      * @return
      **/
     @RequestMapping(value = "/login",method = RequestMethod.POST)
     @ResponseBody
    public ResponseEntity login(HttpServletRequest request){
        String userName = request.getParameter("username");
        String password = request.getParameter("password");
        Map<String,Object> map = loginService.login(userName,password);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(map));

    }


}
